Start serial execution of multiple asynchronous actions.
Syntax
Parameters
- coroutine
- An iterator block containing asynchronous actions
- completedHandler
- Optional completion handler
Return Value
A CoroutineOperation representing this operation
Example
C# | Copy Code |
---|
public void CoroutineSample() {
// Note that a new EM is not required for a Coroutine, we show it here for completeness.
_mgr = new DomainModelEntityManager();
// Start some serial async operations.
var op = Coroutine.Start(SampleActions);
// Listen for completion.
op.Completed += (s, e) => {
if (e.HasError) {
MessageBox.Show(e.Error.Message);
e.MarkErrorAsHandled();
}
};
}
private EntityManager _mgr;
// A block of asynchronous actions.
private IEnumerable<INotifyCompleted> SampleActions() {
// Start a query for all customers in specified country, yield when async op completes.
var op1 = _mgr.Customers.Where(c => c.Country == "UK").ExecuteAsync();
yield return op1;
// Resume execution here when op1 completes. Take a look at results from 1st query
TraceFns.WriteLine("Customer count = " + op1.Results.Count().ToString());
// Perform another async quuery for all employees.
var op2 = _mgr.Employees.ExecuteAsync();
yield return op2;
// Resume execution here when op2 completes. See what it returned.
TraceFns.WriteLine("Employee count = " + op2.Results.Count().ToString());
}
/***************************************************************************************/
// Sample 2 - passing arguments to an iterator
public void CoroutineSample2a() {
// Note that a new EM is not required for a Coroutine, we show it here for completeness.
_mgr = new DomainModelEntityManager();
// Start some serial async operations.
var op = Coroutine.Start(() => SampleActions2("USA"));
// Listen for completion.
op.Completed += (s, e) => {
if (e.HasError) {
MessageBox.Show(e.Error.Message);
e.MarkErrorAsHandled();
}
};
}
private IEnumerable<INotifyCompleted> SampleActions2(String country) {
// Start a query for all customers in specified country, yield when async op completes.
var op1 = _em1.Customers.Where(c => c.Country == country).ExecuteAsync();
yield return op1;
// Resume execution here when op1 completes. Take a look at results from 1st query
TraceFns.WriteLine("Customer count = " + op1.Results.Count().ToString());
// Perform another async quuery for all employees.
var op2 = _em1.Employees.ExecuteAsync();
yield return op2;
// Resume execution here when op2 completes. See what it returned.
TraceFns.WriteLine("Employee count = " + op2.Results.Count().ToString());
} |
Remarks
Requirements
Target Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family
See Also